//
//  LGInspectDetailPhotoCell.m
//  HeartDiseaseManage
//
//  Created by 1234 on 2023/11/3.
//

#import "LGInspectDetailPhotoCell.h"

@interface LGInspectDetailPhotoCell ()<UICollectionViewDelegate,UICollectionViewDataSource>
/**   */
@property (nonatomic , strong)UICollectionView *collectionView;
/**   */
@property (nonatomic , strong)UILabel *numberLabel;

@end

@implementation LGInspectDetailPhotoCell

-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        self.backgroundColor = [UIColor clearColor];
        [self.contentView addSubview:self.collectionView];
        [self.contentView addSubview:self.numberLabel];
        [self.collectionView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(self.contentView).offset(viewPix(16));
            make.right.equalTo(self.contentView).offset(-viewPix(16));
            make.top.bottom.equalTo(self.contentView);
            make.height.equalTo(@(viewPix(487)));
        }];
    }
    return  self;
}

-(void)setPhotoArray:(NSArray *)photoArray{
    _photoArray = photoArray;
    self.numberLabel.text = [NSString stringWithFormat:@"1/%ld",photoArray.count];
    [self.collectionView reloadData];
}

-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return self.photoArray.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cell" forIndexPath:indexPath];
    cell.backgroundColor = [UIColor whiteColor];
    UIImageView *photoView = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, Screen_W-viewPix(32), viewPix(487))];
    photoView.contentMode = UIViewContentModeScaleAspectFit;
    [photoView sd_setImageWithURL:[NSURL URLWithString:LGNSString(self.photoArray[indexPath.item])]];
    [cell.contentView addSubview:photoView];
    return cell;
}

-(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{
    NSInteger index = (self.collectionView.contentOffset.x)/(Screen_W-viewPix(33));
    self.numberLabel.text = [NSString stringWithFormat:@"%ld/%ld",index+1,self.photoArray.count];
}

-(UICollectionView *)collectionView{
    if(!_collectionView){
        UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc]init];
        layout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
        layout.itemSize = CGSizeMake(Screen_W-viewPix(32), viewPix(487));
        layout.minimumInteritemSpacing = 0;
        layout.minimumLineSpacing = 0;
        _collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(viewPix(16), 0, Screen_W-viewPix(32), viewPix(487)) collectionViewLayout:layout];
        [_collectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:@"cell"];
        _collectionView.backgroundColor = [UIColor clearColor];
        _collectionView.pagingEnabled = YES;
        _collectionView.dataSource = self;
        _collectionView.delegate = self;
    }
    return _collectionView;
}

-(UILabel *)numberLabel{
    if(!_numberLabel){
        _numberLabel = [UILabel lableWithText:@"" colorString:@"#FFFFFF" textFont:LGFontWeight(13, UIFontWeightMedium) textAlignment:NSTextAlignmentCenter lines:1];
        _numberLabel.frame = CGRectMake((Screen_W-viewPix(64))/2.0, viewPix(447), viewPix(64), viewPix(24));
        _numberLabel.backgroundColor = [UIColor colorWithString:@"#808080"];
        _numberLabel.cornerRidus = viewPix(12);
    }
    return _numberLabel;
}

@end
